<?php

namespace App\Admin\Controllers;

use App\Models\Rule;

use Encore\Admin\Form;
use Encore\Admin\Grid;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

use Illuminate\Support\Facades\Request;


class RuleController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('系统配置');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('系统配置');
            $content->description('管理');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('系统配置');
            $content->description('管理');

            $content->body($this->form());
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Rule::class, function (Grid $grid) {
            $grid->id('ID')->sortable();

            $grid->key('键名');
            $grid->value('键值')->editable();
            $grid->content('描述');

            $grid->created_at('添加时间');

            $grid->disableExport();
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function ($filter) {
                $filter->useModal();
                $filter->like('title', '标题');
                $filter->between('created_at', '添加时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Rule::class, function (Form $form) {
            $form->text('key', '键名')->rules('required');
            $form->textarea('value', '键值')->rules('required');
            $form->UEditor('content', '描述');
        });
    }

}
